﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections;
using System.Collections.Specialized;
using MS.InformationManagement.Support.Constants;

namespace MS.InformationManagement.Support
{
    public class EmailHelper
    {
        public static bool SendEmail(string toField, string subject, string body)
        {
            bool result = false;
            try
            {
                HttpContext tempCtx = HttpContext.Current;
                SPWeb thisWeb = SPControl.GetContextWeb(tempCtx);
                if (SPUtility.IsEmailServerSet(thisWeb))
                {
                    result = SPUtility.SendEmail(thisWeb, true, true, toField, subject, body);
                }
            }
            catch (Exception ex)
            {
                //throw ex;
            }

            return result;
        }

        public static bool SendEmail(string fromLoginName, string toLoginName, string subject, string templateName, Dictionary<string, string> tokenizedValues, SPWeb web)
        {
            bool sent = false;
            if (!SPUtility.IsEmailServerSet(web))
            {
                throw new Exception("El servidor de correo no se encuentra configurado para este sitio");
            }

            string templateContent = GetTemplate(templateName, web);

            CombineTemplate(ref templateContent, tokenizedValues);

            StringDictionary mailHeaders = new StringDictionary();
            mailHeaders.Add("from", GetUserEmail(fromLoginName));
            mailHeaders.Add("to", GetUserEmail(toLoginName));
            mailHeaders.Add("subject", subject.ToUpper());
            mailHeaders.Add("content-type", "text/html");

            sent = SPUtility.SendEmail(web, mailHeaders, templateContent);

            return sent;
        }

        private static void CombineTemplate(ref string template, Dictionary<string, string> values)
        {
            foreach (KeyValuePair<string, string> currentValue in values)
            {
                template = template.Replace(currentValue.Key, currentValue.Value);
            }
        }

        private static string GetTemplate(string templateName, SPWeb web)
        {
            string templateContent = string.Empty;
            try { templateContent = web.GetFileAsString(string.Format("{0}/{1}/{2}.html", web.Url, GlobalConstants.LIBRARY_MAIL_TEMPLATES, templateName)); }
            catch (Exception) { throw new Exception("No se encuentra la plantilla de correo para esta acción"); }
            return templateContent;
        }

        private static string GetUserEmail(string loginName)
        {
            SPFieldUserValue user = PeopleHelper.GetUserByLoginName(loginName);
            if (user == null) { throw new Exception(string.Format("No existe un usuario con el login {0}", loginName)); }

            string userEmail = user.User.Email;
            return userEmail;
        }
    }


}
